<script setup>
import { ref } from 'vue'
const dialogVisible = ref(false)
const isabled = ref()
isabled

const formModel = ref({
  id: '',
  title: '',
  content: '',
  date: ''
})
const rules = {
  title: [
    { required: true, message: '请输入内容', trigger: 'blur' },
    { min: 2, max: 20, message: '长度在 2 到 40 个字符', trigger: 'blur' }
  ],
  content: [{ required: true, message: '请输入内容', trigger: 'blur' }]
}
const form = ref()
const emit = defineEmits(['success'])
import {
  adminnoteinfoAddService,
  adminnoteinfoUpService
} from '@/api/admin/note'

const sureQuire = async () => {
  await form.value.validate()
  const isEdit = formModel.value.id
  if (isEdit) {
    adminnoteinfoUpService(formModel.value)
      .then((response) => {
        if (response.data.status === 200) {
          ElMessage.success(response.data.message)
          dialogVisible.value = false
          emit('success')
        } else {
          dialogVisible.value = false
          ElMessage.error(response.data.message)
        }
      })
      .catch((error) => {
        ElMessage.error(error)
      })
  } else {
    adminnoteinfoAddService(formModel.value)
      .then((response) => {
        if (response.data.status == '200') {
          dialogVisible.value = false
          emit('success')
          ElMessage.success(response.data.message)
        } else {
          dialogVisible.value = false
          ElMessage.error(response.data.message)
        }
      })
      .catch((error) => {
        ElMessage.error(error)
      })
  }
}
const NoQuire = () => {
  dialogVisible.value = false
  form.value.resetFields()
}
const handleClose = () => {
  dialogVisible.value = false
  form.value.resetFields()
}
const handleClearValidate = () => {}

//
const open = (row) => {
  dialogVisible.value = true
  formModel.value = { ...row } //添加的话，重置表单内容
  if (!row.id) {
    const today = new Date()
    const year = today.getFullYear()
    const month = String(today.getMonth() + 1).padStart(2, '0') //一位数的话添加零
    const day = String(today.getDate()).padStart(2, '0')
    formModel.value.date = `${year}-${month}-${day}`
  }
}
// 处理日期变化事件
defineExpose({
  open
})
</script>

<template>
  <el-dialog
    v-model="dialogVisible"
    :title="formModel.id ? '修改' : '添加'"
    width="550"
    :before-close="handleClose"
  >
    <el-form
      class="el-form"
      :rules="rules"
      ref="form"
      :model="formModel"
      :before-close="handleClearValidate"
    >
      <el-form-item prop="title" label="标题" required="true">
        <el-input
          v-model="formModel.title"
          :prefix-icon="User"
          placeholder="请输入"
          style="width: 240px"
        ></el-input>
      </el-form-item>

      <el-form-item prop="date" label="发布时间" required="true">
        <el-date-picker
          v-model="formModel.date"
          type="date"
          value-format="YYYY-MM-DD"
          :size="size"
          @change="handleDateChange"
          style="width: 130px"
          disabled="true"
        />
      </el-form-item>

      <el-form-item prop="content" label="内容" required="true">
        <el-input
          v-model="formModel.content"
          :prefix-icon="User"
          :autosize="{ minRows: 6, maxRows: 10 }"
          type="textarea"
          style="width: 470px"
        ></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="NoQuire">取消</el-button>
        <el-button type="primary" @click="sureQuire"> 确定 </el-button>
      </div>
    </template>
  </el-dialog>
</template>
<style>
.el-form {
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-wrap: wrap;
}
</style>
